package com.example.haosiji.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.example.haosiji.R;
import com.example.haosiji.app.haosijiApp;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class PriceMenu extends Activity{
	
	private ListView listview;
	private ArrayList<Map<String,Object>> mPriceList;
	
	Handler mHandler;
	SimpleAdapter adapter;
	
	protected ProgressDialog mWaitDialog;
	protected boolean isWaitingCanceled = false;
	
	protected haosijiApp mApp;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.pricemenu);
		
		initView();
	}
	
	
private void initView(){
		
		mApp = (haosijiApp) getApplication();
	
	
		mHandler = new Handler(){
			public void handleMessage(Message message){
				
			    mWaitDialog.hide();
				
				switch(message.what){
					case 0:
						Toast.makeText(getApplicationContext(), "系统错误！", Toast.LENGTH_SHORT).show();
						break;
					case 1:
						Bundle b = message.getData();
						String msg = b.getString("data");
						mPriceList.clear();
						try{
							
							JSONArray jsonArray = new JSONArray(msg);
							
							for(int i = 0; i < jsonArray.length(); i++) {
									JSONObject jsonObject = jsonArray.getJSONObject(i);
									
									String startTime = jsonObject.getString("startTime");
									
									String endTime = jsonObject.getString("endTime");
									
									
									String date = startTime.substring(0, 5)+"-"+endTime.substring(0, 5);
									String price = jsonObject.getString("basicPrice")+"元";
									
									Map<String,Object> item = new HashMap<String,Object>(); 
									item.put("time", date);
									item.put("price", price);
									mPriceList.add(item);
									
							}
							
							
						}catch(Exception e){
							
						}
						
						listview.setAdapter(adapter);
				}
			}
		};
		
		
		listview = (ListView)findViewById(R.id.price_listview);
		
		mPriceList = new ArrayList<Map<String,Object>>(); 
		
		adapter = new SimpleAdapter(this,mPriceList,
				R.layout.pricelistview,
				new String[]{"time","price"},
				new int[]{R.id.price_time,R.id.price_num});
		
		
	}
	

	protected void onResume() {
		super.onResume();
		toService();	
	}
	
	
	private void toService(){
		mApp.checkConnection();
		showWaiting(2, "数据载入中，请稍候...");
		
		new Thread(){
			public void run(){
				
				//baseUrl             
			    String baseUrl = "http://192.168.26.18:8080/driver/price/queryAll.json"; 
			    
			    try{
			    	
					HttpPost request = new HttpPost(baseUrl);				
					
					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
					
					request.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8)); 
					
					/*发送请求并返回结果*/
					HttpResponse httpResponse = new DefaultHttpClient().execute(request);
					
					String retSrc = EntityUtils.toString(httpResponse.getEntity());
					
					Log.i("11111111111111111", retSrc);
							
					Message msg = new Message();
					if(TextUtils.isEmpty(retSrc)){
						msg.what = 0;
						mHandler.sendMessage(msg);
							
					}else{
						Bundle b = new Bundle();
						b.putString("data",retSrc);
                        msg.setData(b);
                        
                        msg.what = 1;
                        mHandler.sendMessage(msg);	
					}		
			    	
			    }catch(Exception e){	
			    	e.printStackTrace();
			    }
									       	      
			} 
			      
		}.start();
	}
	
	

	public void showWaiting(int paramInt, String paramString) {
		if (paramInt == 2) {
			if (mWaitDialog == null) {
				mWaitDialog = new ProgressDialog(this);
			}
			this.mWaitDialog
					.setOnCancelListener(new DialogInterface.OnCancelListener() {
						public void onCancel(
								DialogInterface paramAnonymousDialogInterface) {
							PriceMenu.this.isWaitingCanceled = true;
						}
					});
			this.mWaitDialog
					.setOnDismissListener(new DialogInterface.OnDismissListener() {
						public void onDismiss(
								DialogInterface paramAnonymousDialogInterface) {
							PriceMenu.this.isWaitingCanceled = false;
						}
					});
			this.isWaitingCanceled = false;
			mWaitDialog.setProgressStyle(0);
			mWaitDialog.setMessage(paramString);
			mWaitDialog.setCancelable(true);
			mWaitDialog.show();
		}

	}
	
	
	
	
	
	
	
	
}
